package com.usooft.metaTalent.core.platform.service.base;

import com.usooft.framework.core.query.Limit;
import com.usooft.framework.core.query.Page;
import com.usooft.metaTalent.core.platform.entity.Image;
import com.usooft.metaTalent.core.platform.mapper.ImageMapper;
import com.usooft.metaTalent.core.platform.query.ImageQuery;
import com.usooft.metaTalent.core.platform.service.ImageService;
import com.usooft.metaTalent.core.platform.wrapper.ImageWrapper;
import java.lang.Long;
import java.lang.Override;
import java.lang.String;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.lang.NonNull;

public abstract class BaseImageService implements ImageService {
    @Autowired
    private ImageMapper imageMapper;

    @Override
    public Image get(@NonNull Long id) {
        return find(id).orElseThrow();
    }

    @Override
    public Optional<Image> find(@NonNull Long id) {
        return Optional.ofNullable(imageMapper.get(id));
    }

    @Override
    public List<ImageWrapper> list(ImageQuery query) {
        return imageMapper.list(query);
    }

    @Override
    public Page<ImageWrapper> page(ImageQuery query) {
        int rows = imageMapper.count(query.getFilter());
        return new Page<>(rows, rows == 0 ? new ArrayList<>() : imageMapper.list(query));
    }

    protected Optional<Image> findOne(ImageQuery.Filter filter, ImageQuery.Sorts sorts) {
        List<ImageWrapper> wrappers = list(ImageQuery.builder()
                                .filter(filter)
                                .sorts(sorts)
                                .limit(Limit.rowCount(1))
                                .build());
        return wrappers.stream().findFirst().map(Function.identity());
    }

    protected Optional<Image> findOne(ImageQuery.Filter filter) {
        return findOne(filter, ImageQuery.Sorts.builder().build());
    }

    @Override
    public int count(ImageQuery.Filter filter) {
        return imageMapper.count(filter);
    }

    protected Image internalAdd(String name, String pictureUrl, String suffix, Long articleId) {
        Image image = new Image();
        image.setName(name);
        image.setPictureUrl(pictureUrl);
        image.setSuffix(suffix);
        image.setArticleId(articleId);

        image.setCreatedDate(LocalDateTime.now());
        image.setVersion(0);
        image.setIsDelete(false);
        imageMapper.add(image);
        return image;
    }

    protected void internalUpdate(@NonNull Image image) {
        image.setLastModifiedDate(LocalDateTime.now());

        int rows = imageMapper.update(image);
        if (rows == 0) {
            throw new OptimisticLockingFailureException("更新失败。可能图像 (" + image.getId() + ") 的版本 (" + image.getVersion() + ") 已不是最新");
        }
        image.setVersion(image.getVersion() + 1);
    }
}
